///////////////////////////////////////////////////////////////////////////////////////////////////
// GERPGStatus.h
// Tuesday, July 20, 2010
// Copyright ?2009, 2010, 8DWorld, Inc. All rights reserved.
//
// $Description$
///////////////////////////////////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "UGRPGStatus.h"
#include "base/HWBase.h"
#include "Data/StaticUnitDataLoader.h"
#include "Data/StaticDataFactoryIf.h"
#include "Data/Static2DRenderPictureData.h"
#include "Util/MovableTextConstants.h"
#include "Unit/ObserverCliUnit.h"

extern HWBase* GetRootApp();

UGRPGStatus::GERPGStatus(void) :
m_nStatusId(0),
m_pMovablePic(NULL),
m_pSceneNode(NULL),
m_nExistTime(0),
m_ptStart(0,0,0),
m_ptVector(-4, 2, 0)//Test data
{
}

UGRPGStatus::GERPGStatus(int nStatusID) :
m_nStatusId(nStatusID),
m_pMovablePic(NULL),
m_pSceneNode(NULL),
m_nExistTime(0),
m_ptStart(0,0,0),
m_ptVector(0, 0, 50) //Test data
{
}

UGRPGStatus::~UGRPGStatus(void)
{
}


bool GERPGStatus::OnAttach()
{
	ObserverCliUnit* pObs = dynamic_cast<ObserverCliUnit*>(m_pUnit->GetObserver());

	if (pObs==NULL)
	{
		return false;
	}

	m_pSceneNode = pObs->GetSceneNode();
	if(NULL == m_pSceneNode)
	{
		return false;
	}

    //To create sence node
     Ogre::SceneManager* pSceneMgr = GetRootApp()->GetOgreBase()->GetSceneManager();
    if(NULL == pSceneMgr)
        return false;

    IStaticDataLoader* pStaticLoader = g_StaticDataFactory->GetLoader(Static2DRenderPictureData::category);
    if(NULL == pStaticLoader)
        return false;

    Static2DRenderPictureData* p2DPicData = dynamic_cast<Static2DRenderPictureData*>(pStaticLoader->Get(m_nStatusId));  
    if(NULL == p2DPicData)
        return false;

    Vector3 unitPos(Vector3::ZERO);
    Vector3 vct3ScreenPt;
	Ogre::Camera* pCamera = GetRootApp()->GetOgreBase()->GetCamera();
	if (pCamera)
	{
		vct3ScreenPt = pCamera->getProjectionMatrix() * pCamera->getViewMatrix() * unitPos;
	}

    Vector4 area(p2DPicData->m_TexUV.left, p2DPicData->m_TexUV.top, p2DPicData->m_TexUV.right,p2DPicData->m_TexUV.bottom);
    Vector2 init(vct3ScreenPt.x, vct3ScreenPt.y);
    Vector2 sizeTex(p2DPicData->m_TexWidth, p2DPicData->m_TexHeight);
    Vector2 sizeWin(defaultRenderWindowWidth, defaultRenderWindowHeight);


    m_area1 = area;

    p2DPicData = dynamic_cast<Static2DRenderPictureData*>(pStaticLoader->Get(1003));  
    if(NULL == p2DPicData)
        return false;

    Vector4 area2(p2DPicData->m_TexUV.left, p2DPicData->m_TexUV.top, p2DPicData->m_TexUV.right,p2DPicData->m_TexUV.bottom);

    m_area2 = area2;

    if(NULL == m_pSceneNode)
        return false;

    string strName = "UGRPGStatus_";
    strName += StringConverter::toString((int)this);

    m_pMovablePic = new Movable2DPicture(strName ,p2DPicData->m_TexName, vct3ScreenPt, area, init, sizeTex, sizeWin, RENDER_QUEUE_6, 0, 0, 0, false );
    m_pSceneNode->attachObject(m_pMovablePic);
   

    m_nExistTime = m_nTTL;

    return true;
}

bool GERPGStatus::OnDetach()
{
    if(NULL != m_pSceneNode && NULL != m_pMovablePic)
    {
        m_pSceneNode->detachObject(m_pMovablePic->getName());
        SAFE_DELETE(m_pMovablePic);
    }    

    return true;
}
void GERPGStatus::SetStatusId(int nId)
{
    m_nStatusId = nId;
}


bool GERPGStatus::OnUpdate(int nElapse)
{
    //if(!__super::OnUpdate(nElapse))
    //    return false;

    return OnUpdatePosition();
}

bool GERPGStatus::OnUpdatePosition()
{
    if(NULL == m_pSceneNode || NULL == m_pMovablePic)
        return false;
	if (m_nExistTime<=40)
	{
		m_nExistTime++;
		Vector3 ptOffset =  m_ptVector * (Real(m_nExistTime) / 40);
		ptOffset.x = m_ptVector.x;
		m_pMovablePic->SetOffset(ptOffset);
	}

    return true;
}